<template>
  <el-button type="success" plain @click="add">添加</el-button>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="id" width="180" />
    <el-table-column prop="name" label="权限名称" width="180" />
    <el-table-column prop="createdAt" label="创建时间" width="180" />
    <el-table-column prop="updatedAt" label="更新时间" width="180" />
    <el-table-column  width="180" #default="scope">
      <el-button type="danger" plain @click="deletea(scope.row.id)">删除</el-button>
    </el-table-column>

  </el-table>
  <!--添加弹窗-->
  <el-dialog
      v-model="dialogVisible"
      title="添加"
      width="500"
  >
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="权限名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup>
import {ref} from "vue";
import request from "@/request/request.js";
import {ElMessage} from "element-plus";
const dialogVisible=ref(false)
const form=ref({})
const tableData=ref();
function getUserList(){
  request({
    url:'/api/permissions',
    method:'GET',
  }).then((res)=>{
    if (res.data.code==200){
      tableData.value=res.data.data
      console.log(res.data.data)
    }
  })
}
getUserList()
function add(){
  dialogVisible.value=true
}
function onSubmit(){
  request({
    url:'/api/permissions',
    method:'POST',
    data:form.value
  }).then((res)=>{
    if (res.data.code==200){
      ElMessage.success("添加成功")
      dialogVisible.value=false
      getUserList()
    }else {
      ElMessage.error("添加失败")
    }
  })
}
function deletea(id){
  request({
    url:'/api/permissions/6',
    method:'DELETE',
    param:id,
  }).then((res)=>{
    if (res.data.code==200){
      ElMessage.success("删除成功")
      getUserList()
    }else {
      ElMessage.error("删除失败")
    }
  })
}
</script>